package com.zzu.presenter.impl;

import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.zzu.model.bean.DLS;
import com.zzu.model.bean.Delivery;
import com.zzu.model.bean.PackageItem;
import com.zzu.model.bean.PackageRoute;
import com.zzu.model.impl.PackageModelImpl;
import com.zzu.model.model.PackageModel;
import com.zzu.model.service.PackageService;
import com.zzu.presenter.PackageQueryListener;
import com.zzu.presenter.presenter.PackagePresenter;
import com.zzu.utils.ServiceCreator;
import com.zzu.utils.ToastAndDialogUtils;
import com.zzu.view.view.PackageQueryView;

import java.util.List;

import okhttp3.FormBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-08 15:07
 */
public class PackagePresenterImpl implements PackagePresenter {

    private static final String TAG = PackagePresenterImpl.class.getName();
    PackageModel mPackageModel = null;
    Context context = null;

    ProgressDialog dialog = null;

    private PackageService packageService = ServiceCreator.getRetrofit(PackageService.class);

    PackageQueryView mPackageQueryView = null;

    public PackagePresenterImpl(Context context, PackageQueryView packageQueryView){
        this.context = context;
        mPackageQueryView = packageQueryView;
        //加载进度条
        dialog = ToastAndDialogUtils.showDialog(context,"正在加载中");
        mPackageModel = new PackageModelImpl(context);
    }

    //通过区名返回地区代码，然后获得该地区需要揽收的包裹
    @Override
    public void getDLSTask(String twn) {
        mPackageModel.getDLSTask(twn, new PackageQueryListener<List<DLS>>() {
            @Override
            public void getDates(List<DLS> dates) {
                ToastAndDialogUtils.cancelDialog(dialog);
                if(mPackageQueryView != null){
                    if(dates == null || dates.size() == 0){
                        //没有收到数据
                        mPackageQueryView.getNoDate();
                    }else{
                        mPackageQueryView.getDates(dates);
                    }
                }
            }

            @Override
            public void getNoDate() {
                ToastAndDialogUtils.cancelDialog(dialog);
                mPackageQueryView.getNoDate();
            }
        });
    }

    //通过派送人id查询派送任务列表
    @Override
    public void getDeliveryTaskByHandler(String handle) {
        mPackageModel.getDeliveryTaskByHandler(handle, new PackageQueryListener<List<Delivery>>() {
            @Override
            public void getDates(List<Delivery> datas) {
                ToastAndDialogUtils.cancelDialog(dialog);
                if(mPackageQueryView != null){
                    if(datas == null || datas.size() == 0){
                        //没有收到数据
                        mPackageQueryView.getNoDate();
                    }else{
                        mPackageQueryView.getDates(datas);
                    }
                }
            }

            @Override
            public void getNoDate() {
                ToastAndDialogUtils.cancelDialog(dialog);
                mPackageQueryView.getNoDate();
            }
        });
    }

    //查询运输中的包裹
    public void getPackagesInTransit(){
        Call<List<PackageItem>> call = packageService.getPackagesByStatus(3);
        call.enqueue(new Callback<List<PackageItem>>() {
            @Override
            public void onResponse(Call<List<PackageItem>> call, Response<List<PackageItem>> response) {
                ToastAndDialogUtils.cancelDialog(dialog);
                List<PackageItem> datas = response.body();
                //回调方法
                mPackageQueryView.getDates(datas);
            }

            @Override
            public void onFailure(Call<List<PackageItem>> call, Throwable t) {
                ToastAndDialogUtils.cancelDialog(dialog);
                Log.i(TAG,"请求服务器失败"+t);
                mPackageQueryView.getNoDate();
            }
        });

    }

    //通过运单号查询包裹信息
    @Override
    public void getPackageById(String id) {
        mPackageModel.getPackageById(id, new PackageQueryListener<PackageItem>() {
            @Override
            public void getDates(PackageItem data) {
                ToastAndDialogUtils.cancelDialog(dialog);
                if(mPackageQueryView != null){
                    if(data == null){
                        //没有收到数据
                        mPackageQueryView.getNoDate();
                    }else{
                        mPackageQueryView.getDates(data);
                    }
                }
            }
            @Override
            public void getNoDate() {
                ToastAndDialogUtils.cancelDialog(dialog);
                mPackageQueryView.getNoDate();
            }
        });
    }


    //更新包裹状态
    @Override
    public void updatePackageStatus(PackageRoute packageRoute) {

        FormBody formBody = new FormBody.Builder()
                .add("id", packageRoute.getPackageId())
                .add("status", packageRoute.getStatus().toString())
                .add("handler", packageRoute.getHandler().toString())//传入处理人
                .add("transNodeId",packageRoute.getTransNodeId() )
                .add("x", packageRoute.getX().toString())
                .add("y", packageRoute.getY().toString()).build();
        Call<Boolean> call = packageService.updatePackageStatus(formBody);

        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                //包裹状态更新结果
                ToastAndDialogUtils.cancelDialog(dialog);
                Boolean result = response.body();
                if(result == false){
                    ToastAndDialogUtils.showShort(context,"服务器正在维护中，请稍后重试。。。");
                }
                mPackageQueryView.getDates(result);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                //取消对话框
                ToastAndDialogUtils.cancelDialog(dialog);
                //提示
                ToastAndDialogUtils.showShort(context,"请求服务器失败"+t);
                Log.i(TAG,"请求服务器失败"+t);
            }
        });

    }


}
